package com.jbh.apilibrary

import android.util.Log
import com.google.gson.Gson
import com.jbh.apilibrary.api.ApiRequest
import com.jbh.apilibrary.bean.*
import com.jbh.apilibrary.i.ApiCallBack
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class AppData : AppDataMethod {


    companion object {
        private var instance : AppDataMethod? = null
        fun instance():AppDataMethod{
            instance = AppData()
            return instance as AppData
        }
    }



    /**
     * 登录
     */
    override fun loginTask(username: String, password: String, callBack: ApiCallBack<LoginResultBean>) {
        ApiRequest.createService().loginApi(username,password)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<LoginResultBean>>{

                override fun onCompleted() {
                }
                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }
                override fun onNext(t: BaseResponse<LoginResultBean>?) {
                    if (t?.errorCode == 0){
                        callBack.success(t.data)
                    }else{
                        callBack.error(t?.errorMsg.toString())
                    }
                }
            })
    }




    /**
     * 首页banner
     *
     */
    override fun bannerTask(callBack: ApiCallBack<List<BannerResultBean>>) {
        ApiRequest.createService().bannerApi()
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<List<BannerResultBean>>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    e?.run {
                        callBack.error(message.toString())
                    }
                }

                override fun onNext(t: BaseResponse<List<BannerResultBean>>?) {
                    t?.run {
                        if (errorCode==0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(t.errorMsg)
                        }
                    }
                }
            })
    }





    /**
     * 首页文章列表
     */
    override fun indexArticleTask(page:Int,callBack: ApiCallBack<IndexArticleListResult>) {
        ApiRequest.createService().indexArticleApi(page)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<IndexArticleListResult>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    e?.run {
                        callBack.error(message.toString())
                    }
                }
                override fun onNext(t: BaseResponse<IndexArticleListResult>?) {
                    t?.run {
                        if (errorCode==0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(t.errorMsg)
                        }
                    }
                }
            })
    }




    /**
     * 注册
     *
     * @param username
     * @param password
     * @param rePassword
     * @param callBack
     */
    override fun registerTask(username: String, password: String, rePassword: String, callBack: ApiCallBack<BaseResponse<LoginResultBean>>) {
        ApiRequest.createService()
            .registerApi(username,password,rePassword)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<LoginResultBean>>{
                override fun onCompleted() {
                }
                override fun onError(e: Throwable) {
                    callBack.error(e.message.toString())
                }
                override fun onNext(t: BaseResponse<LoginResultBean>) {
                    if (t.errorCode==0){
                        callBack.success(t)
                    }else{
                        callBack.error(t.errorMsg)
                    }
                }
            })
    }



    /**
     * 退出
     *
     */
    override fun loginOutTask(callBack: ApiCallBack<Any>) {
        ApiRequest.createService().logoutApi()
            .apply {
            subscribeOn(Schedulers.io())
            subscribeOn(Schedulers.newThread())
            subscribeOn(AndroidSchedulers.mainThread())
            subscribe(object :Observer<BaseResponse<Any>>{
                override fun onCompleted() {
                }
                override fun onError(e: Throwable) {
                    callBack.error(e.message.toString())
                }
                override fun onNext(t: BaseResponse<Any>) {
                    if (t.errorCode==0){
                        callBack.success(t)
                    }else{
                        callBack.error(t.errorMsg.toString())
                    }
                }
            })
        }
    }



    /**
     * 获取公众号
     */
    override fun wxarticleTask(callBack: ApiCallBack<List<WxarticleBean>>) {
        ApiRequest.createService().let {
            it.wxarticleApi().subscribeOn(Schedulers.io())
                ?.subscribeOn(Schedulers.newThread())
                ?.subscribeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : Observer<BaseResponse<List<WxarticleBean>>> {
                    override fun onCompleted() {
                        Log.v("[onCompleted] - > ","onCompleted")
                    }
                    override fun onError(e: Throwable?) {
                        Log.v("[onError] - > ","t = ${e?.message}")
                        callBack.error(e?.message.toString())
                    }
                    override fun onNext(t: BaseResponse<List<WxarticleBean>>) {
                        Log.v("[wxarticle] - >onNext ","onNext=${Gson().toJson(t)}")
                        callBack.success(t.data)
                    }
                })
        }
    }



    /**
     * 获取某一个公众号历史记录文章
     *
     */
    override fun wxarticle_ListTask(id: Int, page: Int, callBack: ApiCallBack<WxarticleListBean>) {
        ApiRequest.createService().let {
            it.wxarticle_listApi(id , page)
                .subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<BaseResponse<WxarticleListBean>> {
                    override fun onCompleted() {
                        Log.v("[wxarticle]-> Completed","onCompleted")
                    }
                    override fun onError(e: Throwable?) {
                        Log.v("[wxarticle] - >onError ","t = ${e?.message}")
                        callBack.error(e?.message.toString())
                    }

                    override fun onNext(t: BaseResponse<WxarticleListBean>) {
                        Log.v("[wxarticle] - >onNext ","onNext=${Gson().toJson(t.data.datas)}")
                        callBack.success(t.data)
                    }
                })
        }
    }


    /**
     * 获取体系
     *
     */
    override fun mainTreeTask(callBack: ApiCallBack<List<TreeResult>>) {
        ApiRequest.createService()
            .mainTreeApi()
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<List<TreeResult>>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }
                override fun onNext(t: BaseResponse<List<TreeResult>>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }

    /**
     * 提下的文章
     *
     */
    override fun mainTreeArticleApi(page: Int, id: Int, callBack: ApiCallBack<IndexArticleListResult>) {
        ApiRequest.createService().mainTreeArticleApi(page, id)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<BaseResponse<IndexArticleListResult>> {
                override fun onCompleted() {
                }
                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<IndexArticleListResult>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }


    override fun mainNaviTask(callBack: ApiCallBack<List<TreeResult>>) {
        ApiRequest.createService()
            .mainNaviApi()
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<List<TreeResult>>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }
                override fun onNext(t: BaseResponse<List<TreeResult>>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }



    /**
     * 問答
     */
    override fun wenDaTask(page: Int, callBack: ApiCallBack<IndexArticleListResult>) {
        ApiRequest.createService().wendaApi(page)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<IndexArticleListResult>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<IndexArticleListResult>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }


    /**
     * 个人积分
     *
     */
    override fun integralTask(callBack: ApiCallBack<IntegralBean>) {
        ApiRequest.createService().integralApi()
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<IntegralBean>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<IntegralBean>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }


    /**
     * 个人积分列表
     *
     */
    override fun integralListTask(page: Int, callBack: ApiCallBack<CommonListResult<IntegralBean>>) {
        ApiRequest.createService().integralListApi(page)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<CommonListResult<IntegralBean>>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<CommonListResult<IntegralBean>>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }


    /**
     * 新消息
     *
     */
    override fun newMessageTask(page:Int,callBack: ApiCallBack<MessageListResult>) {
        ApiRequest.createService().newMessageApi(page)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<MessageListResult>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<MessageListResult>?) {
                    t?.run {
                        if (errorCode == 0
                            &&
                            t.data!=null
                            &&
                            t.data.pageCount>0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })

    }

    /**
     * 已读消息
     */
    override fun readMessageTask(page:Int,callBack: ApiCallBack<MessageListResult>) {
        ApiRequest.createService().readMessageApi(page)
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<MessageListResult>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<MessageListResult>?) {
                    t?.run {
                        if (errorCode == 0
                            &&
                            t.data!=null
                            &&
                            t.data.pageCount>0
                        ){
                            callBack.success(t.data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })

    }

    /**
     * 消息数量
     *
     */
    override fun messageCountTask(callBack: ApiCallBack<Any>) {

        ApiRequest.createService().messageCountApi()
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseResponse<Any>>{
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    callBack.error(e?.message.toString())
                }

                override fun onNext(t: BaseResponse<Any>?) {
                    t?.run {
                        if (errorCode == 0){
                            callBack.success(t.data)
                        }else{
                            callBack.error(errorMsg)
                        }
                    }
                }
            })
    }
}